<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="添加模板" :visible.sync="dialogVisible" @close="resetForm('addTimeModelForm')" width="400px">
        <el-form label-position="top" :model="addTimeModelForm" :hide-required-asterisk='true' ref="addTimeModelForm" :rules="addTimeModelRules" class="dialog_form">
            <el-form-item label="选择类型" prop="marketing_goal">
                <div class="flexbox flexcenter flexbetween">
                    <div class="time_type_item flexbox flexcenter" :class="{'active':addTimeModelForm.marketing_goal=='LIVE_PROM_GOODS'}" @click="changeType('LIVE_PROM_GOODS')">
                        <img src="../../assets/img/time_type1.svg" alt="" width="38" height="38">
                        <span class="time_type_text">直播带货</span>
                    </div>
                    <div class="time_type_item flexbox flexcenter" :class="{'active':addTimeModelForm.marketing_goal=='VIDEO_PROM_GOODS'}" @click="changeType('VIDEO_PROM_GOODS')">
                        <img src="../../assets/img/time_type2.svg" alt="" width="38" height="38">
                        <span class="time_type_text">短视频带货</span>
                    </div>
                </div>
            </el-form-item>
            <el-form-item label="模板名称" prop="name">
                <el-input v-model="addTimeModelForm.name" placeholder="请输入模板名称"></el-input>
            </el-form-item>
            <el-form-item label="选择广告账户" prop="advertiser_id">
                <template slot="label">
                    <div class="flexbox flexcenter flexbetween" style="width:360px">
                        <span>选择广告账户</span>
                        <el-input v-model="userQuery" clearable style="width:180px" size="mini" prefix-icon="el-icon-search" placeholder="请输入账户名称或者ID"></el-input>
                    </div>
                </template>
                <el-checkbox-group v-model="addTimeModelForm.advertiser_id">
                    <el-checkbox :label="item.advertiser_id" v-for="(item,index) in filteredData(userList)" :key="index">{{item.name}}</el-checkbox>
                </el-checkbox-group>
            </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm('addTimeModelForm')" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm('addTimeModelForm')" :loading="loading" :disabled="loading" class="el-button-width">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            userList: [],
            userQuery: "",
            addTimeModelForm: {
                name: "",
                marketing_goal: "LIVE_PROM_GOODS",
                advertiser_id: [],
            },
            addTimeModelRules: {
                marketing_goal: [
                    {
                        required: true,
                        message: "请选择类型",
                        trigger: "blur",
                    },
                ],
                name: [
                    {
                        required: true,
                        message: "请输入模板名称",
                        trigger: ["blur", "change"],
                    },
                ],
                advertiser_id: [
                    {
                        required: true,
                        message: "选择广告账户",
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },
    created() {
        this.$bus.on("addTimeModel", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.getUserList();
        });
    },
    beforeDestroy() {
        this.$bus.off("addTimeModel");
    },
    methods: {
        //获取账户列表列表
        async getUserList() {
            const { data: res } = await this.$http.post(
                "adminapi/timeinterval/advertiserList"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.userList = res.data;
            }
        },

        // 选择类型
        changeType(type) {
            this.addTimeModelForm.marketing_goal = type;
        },

        // 筛选
        filteredData(list) {
            var list = list,
                query = this.userQuery;

            if (!query) {
                return list;
            }
            query = query.trim();

            list = list.filter(function (item) {
                if (item.name) {
                    if (
                        item.name.indexOf(query) !== -1 ||
                        item.advertiser_id == query
                    ) {
                        return item;
                    }
                }
            });
            return list;
        },

        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "adminapi/timeinterval/templateAdd",
                        this.addTimeModelForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "添加成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.resetForm(formName);
                        this.$parent.getModelList();
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
    components: {},
};
</script>


<style scoped>
.time_type_item {
    width: 174px;
    box-sizing: border-box;
    padding: 16px;
    border: 1px solid #ebebeb;
    border-radius: 2px;
    cursor: pointer;
    font-size: 14px;
    color: #17233d;
}
.time_type_item.active {
    color: #4475ff;
    border-color: #4475ff;
    background-color: rgba(68, 117, 255, 0.05);
}
.time_type_text {
    margin-left: 10px;
}
.el-checkbox {
    display: block;
    margin-right: 0;
}
.el-checkbox-group {
    max-height: 200px;
    overflow-x: hidden;
    overflow-y: auto;
}
</style>
